package com.airline.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.airline.beans.Customer;
import com.airline.beans.Flight;
import com.airline.beans.Journey;
import com.airline.connection.AirlineSystemServiceProxy;
import com.airline.form.DeleteCustomerIDForm;
import com.airline.form.SearchFlightListForm;

public class SearchFlightListAction extends Action{
	
	
	AirlineSystemServiceProxy proxy = new AirlineSystemServiceProxy();
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		SearchFlightListForm flightForm = (SearchFlightListForm)form;
		
		String id = request.getParameter("id");
		
		if(null != id){
		
		String pay = request.getParameter("pay");
		
		flightForm = (SearchFlightListForm) request.getSession().getAttribute("searchFlightListForm");
		
		Journey[] tempList = flightForm.getFlightList();
		
		Journey tempJourney = tempList[Integer.parseInt(id)];
		
		request.getSession().setAttribute("reservation", tempJourney);
		
		
		Customer cust = (Customer) request.getSession().getAttribute("customer");
		
		tempJourney.setCustomerID(cust.getPersonID());
		
		tempJourney.setPaymentOption(pay);
		
		String ticketNo = proxy.createJourney(tempJourney);
		
		if( ticketNo == null){
			
			((SearchFlightListForm)	(form)).setError("Reservation with the following details already exists " );
			return mapping.findForward("failure");
		}
			
		request.setAttribute("ticketNo", ticketNo);
		
		
		return mapping.findForward("done");
		
		}
		
		Flight flight = flightForm.getFlight();
		
		
		flight.setAvailableSeatCount("");
		flight.setBoardTime("");
		flight.setCrewDetails("");
		flight.setDestTime("");
		flight.setFlightID("");
		flight.setFlightNumber("");
		flight.setFlightStatus("");
		flight.setPrice("");
		flight.setTotalSeatCount("");
		
		Journey[] flightList = proxy.searchFlightItineraries(flight, "0", "10");
		
		flightForm.setFlightList(flightList);
		
		request.getSession().setAttribute("searchFlightListForm", flightForm);
	
		return mapping.findForward("success");
	}

}
